/*
 * @Author: liuYang codkly@gmail.com
 * @Date: 2022-10-26 22:55:19
 * @LastEditors: liuYang codkly@gmail.com
 * @LastEditTime: 2022-11-07 10:25:01
 * @FilePath: \coding\blog\blog_server\src\middleware\response.js
 * @Description: 
 * 暂无备注
 */
/**
 * @description: 给ctx 加上成功 失败 返回
 * @param {*} option
 * @return {*}
 */
function jsonResponse(option = {}) {
  return async (ctx, next) => { 
    ctx.success = function (data,msg) {
      ctx.type = option.type || 'json'
      ctx.body = {
        code: option.successCode || 1,
        msg: msg || option.successMsg || '请求成功',
        data: data
      }
    }
    ctx.fail = function (code, msg) { 
      ctx.type = option.type || 'json'
      ctx.body = {
        code: code || option.failCode || 0,
        msg: msg || option.successMsg || '请求失败',
        data: null
      }
    }
    await next()
  }
}

module.exports = jsonResponse